﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;

namespace TsWeb.Reports
{
    public partial class MatchesByTeam : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTeams();
            }
        }

        private void BindTeams()
        {
            foreach (Team team in TsSystem.Instance.GetAllTeams())
            {
                if (team.IsDummyTeam == false)
                {
                    ListItem item = new ListItem(team.Name, team.TeamId.ToString());
                    ComboBoxTeams.Items.Add(item);
                }
            }
           
            ComboBoxTeams.DataBind();
        }

        protected void ComboBoxTeams_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ComboBoxTeams.SelectedItem != null)
            {
                Team team = TsSystem.Instance.GetTeam(Int32.Parse(ComboBoxTeams.SelectedValue));
                List<Match> matches = TsSystem.Instance.GetMatchesForTeam(team);

                GridViewMatches.DataSource = matches;
                GridViewMatches.DataBind();
            }
        }

        protected void ButtonRunReport_Click(object sender, EventArgs e)
        {
            if (ComboBoxTeams.SelectedItem != null)
            {
                Team team = TsSystem.Instance.GetTeam(Int32.Parse(ComboBoxTeams.SelectedValue));
                List<Match> matches = TsSystem.Instance.GetMatchesForTeam(team);

                GridViewMatches.DataSource = matches;
                GridViewMatches.DataBind();
            }



        }

        protected void GridViewMatches_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int winner = Int32.Parse(e.Row.Cells[4].Text);

                    if (winner == -1)
                        e.Row.Cells[4].Text = "Partido sin jugar";
                    if (winner == 0)
                        e.Row.Cells[4].Text = "Empate";
                    if (winner != -1 && winner != 0)
                    {
                        e.Row.Cells[4].Text = TsSystem.Instance.GetTeamWithoutIncludes(winner).Name;
                    }


                }
            }
            catch (Exception)
            { }
        }


    }
}
